/* ====================================================================
 * (#)Header: EntityFactory.java
 * (#)Revision: 
 * (#)Date Created: Mar 24, 2007 12:55:21 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.test.utils;

import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.karolak.entity.Person;
import com.karolak.entity.app.Role;
import com.karolak.entity.app.User;
import com.karolak.entity.constant.Gender;

/**
 * <code>EntityFactory</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class EntityFactory {

	public static Person createPerson() {
		Person entity = new Person();
		return createPerson(entity);
	}

	public static User createUser() {
		return createUser(null);
	}

	public static Person createPerson(Person entity) {
		if (entity == null) {
			entity = new Person();
		}
		long l = System.nanoTime();
		entity.setFirstName("firstName-" + l);
		entity.setLastName("lastName-" + l);
		if (l % 3 == 0) {
			entity.setEmailAddress("emailAddress-" + l);
		}
		if (l % 2 == 0) {
			entity.setGender(Gender.M);
		} else {
			entity.setGender(Gender.F);
		}
		// entity.setAddress(createAdderss(i));
		return entity;
	}

	public static User createUser(User entity) {
		if (entity == null) {
			entity = new User();
			Object o = createPerson(entity);
			entity = (User) o;
		}
		long l = System.nanoTime();
		entity.setUserName("un-" + l);
		entity.setPassword(String.valueOf(l));
		entity.setPassword(entity.getPassword().substring(0, 10));
		return entity;
	}

	public static Role createRole() {
		Role role = new Role();
		StringBuilder str = new StringBuilder("" + System.nanoTime());

		int id = Integer.valueOf(str.reverse().toString().substring(0, 7));
		role.setId(id);
		role.setName("un-" + System.nanoTime());
		// role.setCreateDate(Calendar.getInstance().getTime());
		// role.setUpdateDate(Calendar.getInstance().getTime());

		return role;
	}

	public static Role createRoleAdmin() {
		Role admin = new Role();
		admin.setId(1);
		admin.setName("admin");
		return admin;
	}

	public static Role createRoleManager() {
		Role role = new Role();
		role.setId(2);
		role.setName("manager");
		return role;
	}

	public static Role createRoleUser() {
		Role role = new Role();
		role.setId(3);
		role.setName("user");
		return role;
	}

	// public static Address createAdderss(int i) {
	// Address address = new Address();
	// address.setStreetAddress("street #:" + i);
	// address.setCity("city-" + i);
	// address.setStateProvince("stateProvince-" + i);
	// address.setZipPostal("ZIP-" + i);
	// address.setCountry("Country-" + i);
	// return address;
	// }
	//	
	// public static Student createStudent(int i) {
	// Student entity = new Student();
	// createPerson(entity, i);
	// entity.setStudentNumber(generateStudentNumber());
	// return entity;
	// }
	//	
	// public static Teacher createTeacher(int i) {
	// Teacher entity = new Teacher();
	// createPerson(entity, i);
	// entity.setTeacherNumber(generateTeacherNumber());
	// return entity;
	// }
	//	
	// public static User createUser(int i) {
	// User entity = new User();
	// createPerson(entity, i);
	// entity.setUserName(generateUserName());
	// return entity;
	// }
	//	
	//	
	// public static Role createRole(int i) {
	// Role role = new Role();
	// role.setDescription("description-" + i);
	// role.setName("name-" + i);
	// return role;
	// }

	public static String generateStudentNumber() {
		String number = "SN-" + Math.random();
		if (number.length() > 20) {
			number = number.substring(0, 20);
		}
		return number;
	}

	public static String generateTeacherNumber() {
		String number = "TN-" + Math.random();
		if (number.length() > 20) {
			number = number.substring(0, 20);
		}
		return number;
	}

	public static String generateUserName() {
		String number = "UN-" + Math.random();
		if (number.length() > 20) {
			number = number.substring(0, 20);
		}
		return number;
	}

	// === utility methods === //
	@SuppressWarnings("unchecked")
	public static <T> T createEntity(String className) {
		T entity = null;
		try {
			Class.forName(className);
			Class<T> typeClass = (Class<T>) Class.forName(className);
			entity = (T) typeClass.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return entity;
	}

	public static <T> T set(T entity, Map<String, Object> properties) {
		if (entity == null || properties == null) {
			return entity;
		}

		for (Map.Entry<String, Object> entry : properties.entrySet()) {
			try {
				BeanUtils.setProperty(entity, entry.getKey(), entry.getValue());
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}

		return entity;
	}
}
